function outarray = uint322colcode(inarray)
% uint322colcode  - convert from uint32 to colcode array
%
% FORMAT:       outarray = uint322colcode(inarray)
%
% Input fields:
%
%       inarray     Nx1 (or 1xN) input array of type uint32
%
% Output fields:
%
%       outarray    Nx4 double array
%                   (n, 1) = color index or NaN
%                   (n, 2) = red component (0...255)
%                   (n, 3) = green component (0...255)
%                   (n, 4) = blue component (0...255)
%
% See also colcode2uint32

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin ~= 1 || ...
   ~isa(inarray, 'uint32')
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad/missing input argument supplied.' ...
    );
end

% linearize array
inarray = double(inarray(:));

% produce output array
outarray = zeros(size(inarray, 1), 4);
outarray(:, 1) = NaN;

% index color range and moduli
idxcolormin = 63 * 16777216;
idxcolormax = 64 * 16777216;
gmodulus = 65536;
bmodulus = 256;

% get colors with indices
rgbcolor = ((inarray >= idxcolormin) & (inarray <  idxcolormax));

% set index
outarray(~rgbcolor, 1) = inarray(~rgbcolor);

% fill RGB correctly
inarray = inarray(rgbcolor) - idxcolormin;
outarray(rgbcolor, 2:4) = floor([inarray / 65536, ...
    mod(inarray, gmodulus) / 256, ...
    mod(inarray, bmodulus)]);
